VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "clsXceed"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

Public WithEvents xMain As XceedBackup
Attribute xMain.VB_VarHelpID = -1

Public xRestore As RestoreJob
Public xSelection As FileSelection
Public PositionTop As Long
Public PositionLeft As Long
Public BackupFolder As String
Public ReplaceAll As VbMsgBoxResult

' Remember file selection
Private saSelectedFiles() As String
Private lSelectedFiles As Long
Private saSelectedKeys() As String
Private lSelectedKeys As Long

Private WithEvents xStatus As frmStatus
Attribute xStatus.VB_VarHelpID = -1
Private xMessage As frmMessage
Attribute xMessage.VB_VarHelpID = -1

'
' Properties
'
Property Get SelectedFiles(lIndex As Long) As String
    If lIndex < 0 Or lIndex >= lSelectedFiles Then
        SelectedFiles = ""
    Else
        SelectedFiles = saSelectedFiles(lIndex)
    End If
End Property

Public Sub AddSelectedFiles(sFile As String)
    If lSelectedFiles <= UBound(saSelectedFiles) Then
        saSelectedFiles(lSelectedFiles) = sFile
        lSelectedFiles = lSelectedFiles + 1
    End If
End Sub

Property Let MaxSelectedFiles(lSize As Long)
    If lSize > 0 Then
        ReDim saSelectedFiles(0 To lSize - 1)
    End If
    lSelectedFiles = 0
End Property

Property Get SelectedFilesCount() As Long
    SelectedFilesCount = lSelectedFiles
End Property

Property Get SelectedKeys(lIndex As Long) As String
    If lIndex < 0 Or lIndex >= lSelectedKeys Then
        SelectedKeys = ""
    Else
        SelectedKeys = saSelectedKeys(lIndex)
    End If
End Property

Public Sub AddSelectedKeys(sKey As String)
    If lSelectedKeys <= UBound(saSelectedKeys) Then
        saSelectedKeys(lSelectedKeys) = sKey
        lSelectedKeys = lSelectedKeys + 1
    End If
End Sub

Property Let MaxSelectedKeys(lSize As Long)
    If lSize > 0 Then
        ReDim saSelectedKeys(0 To lSize - 1)
    End If
    lSelectedKeys = 0
End Property

Property Get SelectedKeysCount() As Long
    SelectedKeysCount = lSelectedKeys
End Property

'
' Methods
'
Private Sub Class_Initialize()
    
    'Intialize all the objects that will be used
    
    Set xMain = New XceedBackup
    Set xRestore = New RestoreJob
    Set xSelection = New FileSelection
    Set xStatus = New frmStatus
    Set xMessage = New frmMessage
    ReplaceAll = vbCancel
    
    lSelectedFiles = 0
    lSelectedKeys = 0
    
End Sub

Private Sub Class_Terminate()

    'Release all the objects
       
    Set xMain = Nothing
    Set xRestore = Nothing
    Set xSelection = Nothing
    Unload xStatus
    Set xStatus = Nothing
    Unload xMessage
    Set xMessage = Nothing
    Unload frmHidden
    
End Sub

Private Sub xMain_BeforeRestoringFile(ByVal sFilename As String, ByVal lSize As Long, ByVal xAttributes As XceedBackupLib.bkpFileAttributes, ByVal dtLastModified As Date, ByVal dtLastAccessed As Date, ByVal dtCreated As Date, ByVal lDiskNumber As Long)

    xStatus.lblFileName.Caption = sFilename & "   Size: " & lSize
  
End Sub

Private Sub xMain_FileStatus(ByVal sFilename As String, ByVal lSize As Long, ByVal lBytesProcessed As Long, ByVal nPercentCompleted As Integer)

    xStatus.lblFileName.Caption = sFilename
    xStatus.ItemPercent = nPercentCompleted
   
End Sub

Private Sub xMain_GlobalStatus(ByVal lFilesTotal As Long, ByVal lFilesProcessed As Long, ByVal lFilesSkipped As Long, ByVal nFilesPercent As Integer, ByVal lBytesTotal As Long, ByVal lBytesProcessed As Long, ByVal lBytesSkipped As Long, ByVal nBytesPercent As Integer)
    
    If xMain.CurrentOperation = bcoRecreateCatalogs Then
        frmTwo.Percent = nFilesPercent
    Else
        xStatus.TotalPercent = nBytesPercent
        xStatus.lblPercent.Caption = nBytesPercent & "%"
    End If
    
End Sub

Private Sub xMain_InsertMedia(ByVal sMediaLabel As String, ByVal lDiskNumber As Long, bMediaInserted As Boolean)

    Dim Answer As VbMsgBoxResult

    Answer = MsgBox("Please insert disk number " & Trim(Str(lDiskNumber)) & " of the media labeled """ & sMediaLabel & """", _
                    vbOKCancel + vbInformation)
                    
    If Answer = vbOK Then
        bMediaInserted = True
    End If
    
End Sub

Public Function RecreateCatalogs() As Boolean

    Dim ResultCode As bkpError
    Dim Answer As VbMsgBoxResult

    'Confirm if the catalogs are to be recreated
    
    Answer = MsgBox("You are about to recreate the catalogs. Do you want to continue?", _
                    vbYesNo + vbQuestion)
    
    'If cancel is pressed then the backups are not recreated
    
    RecreateCatalogs = False
    
    'If OK is pressed then recreate the catalogs
    
    If Answer = vbYes Then
        ResultCode = xMain.RecreateCatalogs(xRestore.BackupSetName, BackupFolder)
        If ResultCode = berSuccess Then
            MsgBox "Catalogs have been successfully recreated.", vbOKOnly + vbExclamation
            RecreateCatalogs = True
        Else
            MsgBox "Could not recreate the catalogs. Error code " & Trim(Str(ResultCode)) & " (" & xMain.GetErrorDescription(bvtError, ResultCode) & ")", _
                   vbOKOnly + vbCritical
        End If
    End If

End Function

Private Sub xMain_InvalidPassword(ByVal sBackupFilename As String, sPassword As String, bPasswordProvided As Boolean)

    'Triggered if an invalid password was given
    
    sPassword = InputBox("The password you have provided for the backup set " & sBackupFilename & " is invalid. Please enter a new one.", "Recovery Wizard")
    
    'If cancel is pressed, the input box returns an empty string.
    'If a valid password is entered then set bPasswordProvided to True
    'If an invalid password is provided, it will again trigger the
    'InvalidPassword event
    
    If sPassword = "" Then
        bPasswordProvided = False
    Else
        bPasswordProvided = True
    End If

End Sub

Private Sub xMain_ProcessCompleted(ByVal lFilesTotal As Long, ByVal lFilesProcessed As Long, ByVal lFilesSkipped As Long, ByVal lBytesTotal As Long, ByVal lBytesProcessed As Long, ByVal lBytesSkipped As Long, ByVal xResult As XceedBackupLib.bkpError)

    If xMain.CurrentOperation = bcoManualRestore Then
        xStatus.lstSkipped.AddItem ("==============================")
        xStatus.lstSkipped.AddItem ("Total number of files : " & lFilesTotal)
        xStatus.lstSkipped.AddItem ("Files processed : " & lFilesProcessed)
        xStatus.lstSkipped.AddItem ("Files Skipped : " & lFilesSkipped)
        xStatus.lstSkipped.AddItem ("===============================")
        xStatus.lstSkipped.AddItem ("Result : " & xResult & " - " & xMain.GetErrorDescription(bvtError, xResult))
    End If
    
End Sub

Private Sub xMain_ReplacingFile(ByVal sFilename As String, ByVal lExistingSize As Long, ByVal xExistingAttributes As XceedBackupLib.bkpFileAttributes, ByVal dtExistingLastModified As Date, ByVal dtExistingLastAccessed As Date, ByVal dtExistingCreated As Date, ByVal lSize As Long, ByVal xAttributes As XceedBackupLib.bkpFileAttributes, ByVal dtLastModified As Date, ByVal dtLastAccessed As Date, ByVal dtCreated As Date, ByVal lDiskNumber As Long, bSkipFile As Boolean)
    
    Dim Answer As VbMsgBoxResult
    
    'ReplaceAll is used to avoid asking for each file to replace
    If ReplaceAll = vbCancel Then
        ReplaceAll = MsgBox("At least one file being restored already exists. Do you want to replace all the files?", _
                            vbYesNo + vbQuestion)
    End If
    
    If ReplaceAll = vbYes Then
        bSkipFile = False
    Else
        Answer = MsgBox("Do you wish to replace " & sFilename & " ?", vbYesNo + vbQuestion)
        If Answer = vbYes Then
            bSkipFile = False
        End If
    End If
   
End Sub

Private Sub xMain_ReplacingRegKey(ByVal sRegKey As String, xAction As XceedBackupLib.bkpReplacingRegKeyAction)

    Dim xActionMessage As enuActionMessage
    
    xMessage.lblRegKey = sRegKey
    'ShowForm from frmMessage returns enuActionMessage which will determine wether
    'the Registry keys will be Skipped, Merged or Overwritten. And if it is applied to all
    
    xActionMessage = xMessage.ShowForm
    
    If xActionMessage <> maAbort Then
        xAction = xActionMessage
    Else
        xMain.Abort = True
    End If

End Sub

Private Sub xMain_RestoringRegKey(ByVal sRegKey As String)

    xMessage.lblRegKey.Caption = sRegKey
    
End Sub

Private Sub xMain_SkippingFile(ByVal sFilename As String, ByVal lSize As Long, ByVal xAttributes As XceedBackupLib.bkpFileAttributes, ByVal dtLastModified As Date, ByVal dtLastAccessed As Date, ByVal dtCreated As Date, ByVal xSkippingReason As XceedBackupLib.bkpSkippingReason)

    xStatus.lstSkipped.AddItem (sFilename & "   Reason : " & xSkippingReason & " " & xMain.GetErrorDescription(bvtSkippingReason, xSkippingReason))
        
End Sub

Private Sub xMain_SkippingRegKey(ByVal sRegKey As String, ByVal xSkippingReason As XceedBackupLib.bkpSkippingReason)

    xStatus.lstSkipped.AddItem (sRegKey & "   Reason : " & xSkippingReason & " " & xMain.GetErrorDescription(bvtSkippingReason, xSkippingReason))
    
End Sub

Public Function RestoreBackup() As Boolean

    ReplaceAll = vbCancel
    xStatus.ShowForm

End Function

Private Sub xStatus_Cancel()
    
    If MsgBox("Are you sure you want to abort the restoration?", vbYesNo + vbQuestion) = vbYes Then
        xMain.Abort = True
    End If
    
End Sub

Private Sub xStatus_StartRestore()

    Dim ResultCode As bkpError
            
    ' Fill the FilesToProcess and RegKeysToInclude
    Dim i As Integer
    
    xSelection.FilesToProcess = ""
    For i = 0 To lSelectedFiles - 1
        xSelection.AddFilesToProcess saSelectedFiles(i), False
    Next i
    
    xSelection.RegKeysToInclude = ""
    For i = 0 To lSelectedKeys - 1
        xSelection.AddRegKeysToInclude saSelectedKeys(i)
    Next i
    
    'The file selection is passed to xRestore and the restoration of the backups can start
    
    xRestore.Selection = xSelection
    ResultCode = xMain.Restore(xRestore)
    
    'Checks the return code
    
    If ResultCode = 0 Then
        MsgBox "All files have been successfully restored.", vbOKOnly + vbExclamation
    Else
        MsgBox "Restore failed. Error code " & Trim(Str(ResultCode)) & " (" & xMain.GetErrorDescription(bvtError, ResultCode) & ")", _
               vbOKOnly + vbCritical
    End If
             
End Sub

